package com.dzt.androidframework.network.mobapi.cook;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dzt.androidframework.R;
import com.dzt.androidframework.adapter.AbsListAdapter;
import com.dzt.androidframework.network.mobapi.cook.CookMenu.ResultBean.ListBean;
import com.dzt.androidframework.recycler.RecyclerAdapter;
import com.dzt.androidframework.recycler.RecyclerHolder;
import com.dzt.androidframework.utils.GsonUtils;

import java.util.List;

/**
 * Created by M02323 on 2017/9/16.
 */

public class CookMenuAdapter extends RecyclerAdapter<ListBean> {

	public CookMenuAdapter(Context context, List<ListBean> datas, int layoutId) {
		super(context, datas, layoutId);
	}

	@Override
	public void convert(RecyclerHolder holder, ListBean data, int position) {
		holder.setText(R.id.tv_ctg_title, data.getCtgTitles());
		holder.setText(R.id.tv_name, data.getName());
		holder.setText(R.id.tv_ingredients, data.getRecipe().getIngredients());
		ListView lvMethod = holder.findView(R.id.lv_method);
		List<Method> list = GsonUtils.jsonToList(data.getRecipe().getMethod(), Method.class);
		if(list == null){
			lvMethod.setVisibility(View.GONE);
		}else{
			MethodAdapter adapter = new MethodAdapter(context, list);
			lvMethod.setAdapter(adapter);
		}
		holder.setText(R.id.tv_sumary, data.getRecipe().getSumary());
	}

	class MethodAdapter extends AbsListAdapter<Method> {

		public MethodAdapter(Context context, List<Method> datas) {
			super(context, datas);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = layoutInflater.inflate(R.layout.item_method, parent, false);
				holder.img = (ImageView) convertView.findViewById(R.id.iv_icon);
				holder.step = (TextView) convertView.findViewById(R.id.tv_step);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			Method method = datas.get(position);
			if (TextUtils.isEmpty(method.getImg())) {
				holder.img.setVisibility(View.GONE);
			} else {
				Glide.with(context).load(method.getImg())
						.placeholder(R.mipmap.ic_launcher)
						.crossFade()
						.error(R.drawable.pic_loading)
						.into(holder.img);
			}
			holder.step.setText(method.getStep());
			return convertView;
		}

		class ViewHolder {
			ImageView img;
			TextView step;
		}
	}
}
